<?php
require_once(dirname(__FILE__) . '/../../inc/config.inc.php');
date_default_timezone_set('PRC');
require_once('PHPExcel.php');


$input_month_str =isset($_GET['m']) ? htmlspecialchars($_GET['m']) : "" ;

if( strtotime($input_month_str)<1575043200 ){
    exit("日期格式不正确");
}
$input_month = date("m", strtotime($input_month_str));

$today_date = date("Y年m月d日");




function getMonthweeks($date)
{
    $ret = array();
    $stimestamp = strtotime($date);
    $mdays = date('t', $stimestamp);
    $msdate = date('Y-m-d', $stimestamp); //月度开始日期

    $medate = date('Y-m-' . $mdays, $stimestamp); // 月度结束日期
    $etimestamp = strtotime($medate);
    //获取第一周
    $zcsy = 7 - date('w', $stimestamp); //第一周去掉第一天还有几天

    $zcs1 = $msdate;
    $zce1 = date('Y-m-d', strtotime("+$zcsy day", $stimestamp));
    $first_week = ["第1周", $zcs1, $zce1];
    $first_week['data'] = getDaysList($zcs1, $zce1);
    //计算第一周里面的天数

    $ret[] = $first_week;
    //获取中间周次
    $jzc = 0;
    //获取月份是6周次还是5周次
    $jzc0 = 0;
    $jzc6 = 0;
    for ($i = $stimestamp; $i <= $etimestamp; $i += 86400) {
        // 0（表示星期天）到 6（表示星期六）
        if (date('w', $i) == 0) {
            $jzc0++;
        }
        if (date('w', $i) == 6) {
            $jzc6++;
        }
    }
    if ($jzc0 == 5 && $jzc6 == 5) {
        $jzc = 5;
    } else {
        $jzc = 4;
    }


    $t = strtotime('+1 monday ',strtotime($msdate));



    if(date('w', $stimestamp)==1){
        for ($n = 2; $n <= $jzc; $n++) {
            $b = strtotime("+$n week -1 week", $t);
            $dsdate = date("Y-m-d", strtotime("0 day", $b));
            $dedate = date("Y-m-d", strtotime("6 day", $b));

            $item_week = ["第" . ($n + 1) . "周", $dsdate, $dedate];
            $item_week['data'] = getDaysList($dsdate, $dedate);
            $ret[] = $item_week;

        }
    }else{
        for ($n = 1; $n < $jzc; $n++) {
            $b = strtotime("+$n week -1 week", $t);
            $dsdate = date("Y-m-d", strtotime("0 day", $b));
            $dedate = date("Y-m-d", strtotime("6 day", $b));

            $item_week = ["第" . ($n + 1) . "周", $dsdate, $dedate];
            $item_week['data'] = getDaysList($dsdate, $dedate);
            $ret[] = $item_week;

        }
    }



    //最后最后一周
    $zcsy = date('w', $etimestamp) - 1;//最後一周是周几 日~六 0~6
    if($zcsy>=0){
        $zcs1 = date('Y-m-d', strtotime("-$zcsy day", $etimestamp));
        $zce1 = $medate;
        $jzcz = $jzc + 1;
        $end_week = ["第" . $jzcz . "周", $zcs1, $zce1];
        $end_week['data'] = getDaysList($zcs1, $zce1);
        $ret[] = $end_week;
    }
    return $ret;
}


function getDaysList($startDate, $endDate)
{
    $item = [];
    $startDate = strtotime($startDate);
    $endDate = strtotime($endDate);
    $weekarray = array("日", "一", "二", "三", "四", "五", "六");
    for ($i = $startDate; $i <= $endDate; $i += 86400) {
        // 0（表示星期天）到 6（表示星期六）

        $item_day['day'] = date("Y-m-d", $i);
        $item_day['week_name'] = "星期" . $weekarray[date("w", $i)];
        $item[] = $item_day;
    }
    return $item;
}




$ret = getMonthweeks($input_month_str);


var_export($ret);

?>

